package com.njupt.service;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;

import com.njupt.dao.PaginationSupport;



public interface UniversalManager
{
	List getAll(Class clazz);

    /**
     * Generic method to get an object based on class and identifier. 
     * 
     * @param clazz model class to lookup
     * @param id the identifier (primary key) of the class
     * @return a populated object 
     * @see org.springframework.orm.ObjectRetrievalFailureException
     */
    Object get(Class clazz, Serializable id);

    /**
     * Generic method to save an object.
     * @param o the object to save
     * @return a populated object
     */
    Object save(Object o);

    /**
     * Generic method to delete an object based on class and id
     * @param clazz model class to lookup
     * @param id the identifier of the class
     */
    void remove(Class clazz, Serializable id);
    
    List findByExample(Object o);
    List findByHql(String hql);
    List findByHql(String hql, Object[] values);
    List find(String hql);
    List find(String hql, Object[] values);
    int executeUpdate(String query);
    
    int executeUpdate(String query, Object value);
    int executeUpdate(String query, Object[] values);
	<T> Criteria createCriteria(Class<T> entityClass, Criterion... criterions);

	<T> T findUniqueBy(Class<T> entityClass, String propertyName, Object value);
	List findAllByCriteria(DetachedCriteria detachedCriteria);
	/**
	 * @param detachedCriteria
	 * @return
	 */
	abstract PaginationSupport findPageByCriteria(
			DetachedCriteria detachedCriteria);

	/**
	 * @param detachedCriteria
	 * @param startIndex
	 * @return
	 */
	abstract PaginationSupport findPageByCriteria(
			DetachedCriteria detachedCriteria, int startIndex);

	/**
	 * @param detachedCriteria
	 * @param pageSize
	 * @param startIndex
	 * @return
	 */
	abstract PaginationSupport findPageByCriteria(
			DetachedCriteria detachedCriteria,
			int startIndex, int pageSize);

	/**
	 *
	 * 
	 * @param detachedCriteria
	 * @return
	 */
	abstract int getCountByCriteria(
			DetachedCriteria detachedCriteria);
	
	/**
	 * 
	 * @param example
	 * @param start
	 * @return
	 */
	PaginationSupport findByExample(Object example, int start);

	PaginationSupport findPageByHql(String hql, int startIndex);

	PaginationSupport findPageByHql(String hql, int startIndex, int pageSize);

	PaginationSupport findPageByHql(String hql, int startIndex, Object[] values);

	PaginationSupport findPageByHql(String hql, int startIndex, int pageSize, Object[] values);

	PaginationSupport findPageByHql(String hql, String countHql, int startIndex, Object[] values);

	PaginationSupport findPageByHql(String hql, String countHql, int startIndex, int pageSize, Object[] values);
	List findByNativeSql(String nativeSql,Class[] entities,Object[] params);
	List findByNativeSql(String nativeSql,String[] alias,Class[] entityTypes,Object[] params);
	List findByNativeSqlAsMap(String nativeSql,Object[] params);
	/**
	 * 执行一条原始sql，并进行分页。
	 * 
	 * @param nativeSql sql语句
	 * @param entities 转换成什么实体类
	 * @param params 参数
	 * @param startIndex 开始记录
	 * @param pageSize 查出多少条
	 * @return
	 */
	PaginationSupport paginateByNativeSql(String nativeSql,Class[] entities,Object[] params,
			int startIndex, int pageSize	
			);
	PaginationSupport paginateByNativeSql(String nativeSql,Class entityType,Object[] params,
			int startIndex, int pageSize	
			);
	PaginationSupport paginateByNativeSql(String nativeSql,Object[] params,
			int startIndex, int pageSize	
			);
	/**
	 * 分页，并且把查询结果转化为Map的list。
	 * 
	 * @param nativeSql
	 * @param params
	 * @param startIndex
	 * @param pageSize
	 * @return
	 */
	PaginationSupport paginateAsMapByNativeSql(String nativeSql,
			Object[] params, int startIndex, int pageSize);
	PaginationSupport paginateByCriteria(DetachedCriteria dc, int pageNo,
			int pageSize);
	/**
	 * 针对返回一条数据的情况。
	 * 
	 * @param hql
	 * @param params
	 * @return
	 */
	Object queryUniqueResult(String hql, Object... params);
	/**
	 * 执行一个DetachedCriteria，获取单条数据。
	 * @param detachedCriteria
	 * @return
	 */
	Object queryUniqueResult(DetachedCriteria detachedCriteria);
	/**
	 * 
	 * @param nativeSql
	 * @param alias
	 * @param entityTypes
	 * @param params
	 * @param startIndex
	 * @param pageSize
	 * @return
	 */
	PaginationSupport paginateByNativeSql(String nativeSql,
			String[] alias,Class[] entityTypes,Object[] params,
			int pageNo, int pageSize
			);
	/**
	 * 根据页码和hql分页。
	 * @param hql
	 * @param pageNo
	 * @param pageSize
	 * @param params
	 * @return
	 */
	PaginationSupport paginateByHql(
			 String hql,
			 int pageNo, int pageSize,Object ... params);
	/**
	 * 执行修改数据的sql。
	 * @param nativeSql
	 * @param params
	 * @return
	 */
	int executeUpdateByNativeSql(String nativeSql, Object[] params);

}
